<template>
  <div class="sub-category">
    <div class="container">
      <!-- 面包屑 -->
      <SubBread></SubBread>
      <!-- 筛选区 -->
      <SubFilter @filter-change="filterChange"></SubFilter>
      <!-- <XtxCheckbox v-model="isFullChecked">全选</XtxCheckbox> -->
      <div class="goods-list">
        <!-- 排序 -->
        <SubSort @sort-change="sortChange"></SubSort>
        <!-- 列表 -->
        <ul>
          <li v-for="goods in goodsList" :key="goods.id">
            <GoodsItem :goods="goods"></GoodsItem>
          </li>
        </ul>
        <!-- 正在加载/没有更多啦 -->
        <XtxInfiniteLoading @infinite="getData" :loading="loading" :finished="finished"></XtxInfiniteLoading>
      </div>
    </div>
  </div>
</template>

<script>
import SubBread from './components/sub-bread.vue'
import SubFilter from './components/sub-filter.vue'
import SubSort from './components/sub-sort.vue'
import GoodsItem from './components/goods-item.vue'
import XtxInfiniteLoading from '../../components/library/xtx-infinite-loading.vue'
import { findSubCategoryGoods } from '../../api/category.js'
import { ref } from '@vue/reactivity'
import { useRoute } from 'vue-router'
import { watch } from '@vue/runtime-core'
// import XtxCheckbox from '@/components/library/xtx-checkbox.vue'

export default {
  name: 'SubCategory',
  components: { SubBread, SubFilter, SubSort, GoodsItem, XtxInfiniteLoading },
  setup () {
    const route = useRoute()
    // 是否加载中
    const loading = ref(false)
    // 是否所有数据加载完毕
    const finished = ref(false)
    // 商品数据列表
    const goodsList = ref([])
    // 请求参数
    let reqParams = {
      page: 1,
      pageSize: 20
    }
    const getData = () => {
      // console.log('加载数据')
      loading.value = true
      // 设置二级分类的id
      reqParams.categoryId = route.params.id
      findSubCategoryGoods(reqParams).then(({ result }) => {
        // 获取数据成功
        if (result.items.length) {
          goodsList.value.push(...result.items)
          reqParams.page++
        } else { // 没有数据（加载完成）
          finished.value = true
        }
        loading.value = false
      })
    }

    // 切换二级分类的时候 需要重新加载数据
    watch(() => route.params.id, (newval) => {
      if (newval && ('/category/sub/' + newval) === route.path) {
        goodsList.value = []
        reqParams = {
          page: 1,
          pageSize: 20
        }
        finished.value = false
      }
    })

    // 1.更改排序组件的筛选数据，重新请求
    const sortChange = (sortParams) => {
      // console.log(sortParams)
      finished.value = false
      // 合并请求参数，保留之前的
      reqParams = { ...reqParams, ...sortParams }
      reqParams.page = 1
      goodsList.value = []
    }
    // 2.更改筛选组件的筛选数据，重新请求
    const filterChange = (filterParams) => {
      // console.log(filterParams)
      finished.value = false
      // 合并请求参数，保留之前的参数
      reqParams = { ...reqParams, ...filterParams }
      reqParams.page = 1
      goodsList.value = []
    }
    return { loading, finished, goodsList, getData, sortChange, filterChange }
  }
}
</script>

<style lang="less" scoped>
.goods-list{
  background-color: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul{
    display: flex;
    flex-wrap: wrap;
    padding:0 5px;
    li{
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n){
        margin-right:0;
      }
    }
  }
}
</style>
